function add(x,y){
    return x + y;
}

function square(z){
    return z * z;
}


function compose(mids){
    return (...args) => {
        let res = mids[0](...args);

        for(let i = 1; i<mids.length; i++){
            res = mids[i](res);
        }

        return res;
    }
}


function composeAsync(mids){

    return function(){
        
        return dispatch(0);

        function dispatch(i){
            let fn = mids[i];
            if(!fn){
                return Promise.resolve();
            }

            return Promise.resolve(fn(function next(){
                return dispatch(i + 1);
            }))
        }
    }
}